package com.zpy.controller;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zpy.pojo.Role;
import com.zpy.pojo.Unit;
import com.zpy.pojo.User;
import com.zpy.pojo.UserRole;
import com.zpy.service.RoleService;
import com.zpy.service.UnitService;
import com.zpy.service.UserRoleService;
import com.zpy.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import java.awt.print.Book;
import java.io.File;
import java.io.IOException;
import java.io.Reader;
import java.util.ArrayList;
import java.util.List;

@Slf4j
@Controller
@RequestMapping("user")
public class UserController {
    @Autowired
    private UserService userService;
    @Autowired
    private UserRoleService userRoleService;
    @Autowired
    private RoleService roleService;
    @Autowired
    private UnitService unitService;
    @Value("${location}")
    private String location;
    @RequestMapping("listUser")
    public String listBook(@RequestParam(required = false,value ="pageNum",defaultValue = "1")Integer pageNum,
                           @RequestParam(required = false,value ="pageSize",defaultValue = "10")Integer pageSize, Model model, User user){

        if (pageNum<0 || pageNum.equals("") || pageNum==null){
            pageNum=1;
        }
        if (pageSize<0 || pageSize.equals("") || pageSize==null){
            pageSize=10;
        }
        PageHelper.startPage(pageNum,pageSize);
        QueryWrapper<User>qw=new QueryWrapper<>();
        if (user.getUsername()!=null){
            qw.like("username",user.getUsername());
        }
        List<User> list = userService.list(qw);
        for (User user1 : list) {
            QueryWrapper<UserRole>queryWrapper=new QueryWrapper<>();
            queryWrapper.eq("uid",user1.getId());
            UserRole one = userRoleService.getOne(queryWrapper);
            Role byId = roleService.getById(one.getRid());
            user1.setRole(byId.getRoleName());
        }
        PageInfo<User> pageInfo = new PageInfo<>(list);
        model.addAttribute("pageInfo",pageInfo);
        return "user-list";

    }

    //跳转到添加用户的页面
    @RequestMapping("preSaveUser")
    public String preSaveReader(Model model){
        List<Unit> list = unitService.list(null);
        model.addAttribute("unitList",list);
        return "user-save";
    }
    //添加用户
    @RequestMapping("saveUser")
    public String save(User user){
        user.setUsername(user.getNumber());
        boolean save = userService.save(user);
        QueryWrapper<User>qw=new QueryWrapper<>();
        qw.eq("number",user.getNumber());
        User one = userService.getOne(qw);
        boolean b=userRoleService.saveUserRole(one.getId());
        return "redirect:/user/listUser";
    }



    //跳转到修改用户的页面
    @RequestMapping("preUpdateUser/{id}")
    public String preUpdateReader(@PathVariable Integer id,Model model){
        List<Unit> list = unitService.list(null);
        model.addAttribute("unitList",list);
        User user = userService.getById(id);
        model.addAttribute("user",user);
        return "user-update";
    }

    //修改用户
    @RequestMapping("updateUser")
    public String updateReader(User user){
        boolean b = userService.updateById(user);
        return "redirect:/user/listUser";
    }

    //删除用户
    @RequestMapping("delUser/{id}")
    public String delReader(@PathVariable Integer id){
        boolean b = userService.removeById(id);
        return "redirect:/user/listUser";
    }

    //详情
    @RequestMapping("detail/{id}")
    public String detail(@PathVariable Integer id,Model model){
        User byId = userService.getById(id);
        model.addAttribute("user",byId);
        return "user-detail";
    }
}
